<template>
  <!-- 最外层的容器 -->
  <view class="my-checkout-container">
    <!-- 全选区域 -->
    <label class="radio" @click="checkAllHandler">
      <radio color="#C00000" :checked="isCheckedAll" />
      <text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{ checkedAmount }}</text>
    </view>

    <!-- 结算按钮 -->
    <view class="btn-checkout" @click="checkout">结算({{ checkedTotal }})</view>
  </view>
</template>

<script>
import { mapGetters, mapState } from "vuex"
export default {
  data() {
    return {
      // 倒计时的秒数
      seconds: 3,
      // 倒计时定时器
      timer: null,
    }
  },
  computed: {
    ...mapGetters("cart", ["checkedTotal", "total", "checkedAmount"]),
    ...mapGetters("user", ["fullAddress"]),
    ...mapState("user", ["token"]),
    ...mapState('cart', ['cartItems']),
    isCheckedAll() {
      return this.checkedTotal === this.total
    },
  },
  methods: {
    // 结算
    async checkout() {
      // 1.判断是否勾选商品
      if (this.checkedTotal === 0) return this.$showMsg("请勾选商品")
      // 2.判断是否选择收货地址
      if (!this.fullAddress) return this.$showMsg("请选择收货地址")
      // 3.是否已登录
      if (!this.token) return this.delayNavigate()
      console.log("可以结算了...")
      // 1.计算出<选中>的商品列表信息 [ { goods_id: xx, goods_count:xx, goods_price: xxx } ]
      const goods = this.cartItems
                    .filter(item => item.goods_state)
                    // .map(({ goods_id,goods_count, goods_price  }) => ({goods_id,goods_count, goods_price}))
                    .map(item => {
                      return {
                        goods_id: item.goods_id,
                        goods_count: item.goods_count,
                        goods_price: item.goods_price
                      }
                    })

      const { meta: {status}, message } = await this.$http.post('/my/orders/create', {
        order_price: 0.01, // 支付的价格
        consignee_addr: this.fullAddress, // 详细地址
        goods  // 商品信息
      })
      if (status !== 200) return this.$showMsg()
      console.log(message);
    },
    delayNavigate() {
      // 1. 重置秒数
      this.seconds = 3

      // 2. 初次展示提示消息
      this.$showMsg("请登录后再结算！" + this.seconds + " 秒后自动跳转到登录页")

      // 3. 创建定时器
      this.timer = setInterval(() => {
        // 3.1 递减秒数
        this.seconds--

        // 3.2 判断秒数 <= 0 则清除定时器并跳转
        if (this.seconds <= 0) {
          clearInterval(this.timer)
          uni.switchTab({ url: "/pages/my/my" })
          // 记录当前页面的信息 type + url
          this.$store.commit('user/setRedirectInfo', {
            type: 'tab',  // 跳转的类型
            url: '/pages/cart/cart'  // 跳转的路径
          })
        } else {
          // 3.3 否则继续显示倒计时信息 
          this.$showMsg("请登录后再结算！" + this.seconds + " 秒后自动跳转到登录页")
        }
      }, 1000)
    },
    checkAllHandler() {
      this.$store.commit("cart/updateAllState", !this.isCheckedAll)
    },
  },
}
</script>

<style lang="scss">
.my-checkout-container {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 999;
  width: 100%;
  height: 100rpx;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10rpx;
  font-size: 28rpx;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #c00000;
  }

  .btn-checkout {
    height: 100rpx;
    min-width: 200rpx;
    background-color: #c00000;
    color: white;
    line-height: 100rpx;
    text-align: center;
    padding: 0 20rpx;
  }
}
</style>